package cn.edu.tit.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * 删除用户（根据编号来删除数据）
 * @author liangyi
 *
 */
public class DeleteUser {
    public static void main(String[] args) {
    	Scanner scanner = new Scanner(System.in);
    	System.out.println("请输入用户编号，该编号用户将被删除：");
    	String userid = scanner.next();
    	Connection connection = null;
    	
    	try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/titdb";
			connection = DriverManager.getConnection(url,"root","root");
			String sql = "DELECT FROM users WHERE userid=?";
			PreparedStatement pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, userid);
			int flag = pstmt.executeUpdate();
			if (flag != 0) {
				System.out.println("删除成功！");
			} else {
				System.out.println("删除失败！");
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    }
}
